package com.klxedu.ms.gateway.core.json;

import java.util.List;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

/**
 * 
 * @author LiuHG
 * @version 1.0
 */
public class JsonBindingErrorObject extends JsonErrorObject<Object>{
	
	public static final String MESSAGE_SEPARATOR = ";";

	public JsonBindingErrorObject(BindingResult result) {
		this(result.getFieldErrors());
	}
	
	public JsonBindingErrorObject(List<FieldError> errors) {
		if(errors == null || errors.isEmpty()) {
			throw new RuntimeException("BindingResult not error");
		}
		super.setCode(ResponesCode.ERROR_BINDING_ERROR);
		StringBuilder strBuilder = new StringBuilder();
		for (FieldError fieldError : errors) {
			String errorMessage = fieldError.getField() + fieldError.getDefaultMessage()+MESSAGE_SEPARATOR;
			strBuilder.append(errorMessage);
		}
		super.setMessage(strBuilder.toString());
	}
}
